// pages/Goods/goodsOrderDetail/index.js
const common = require("../../../../public.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    id: 0,
    orderDes: {},
    code: true,
    storeInfo: {},
    isLoading: false,
    showRefundDialog: false,
    refundRadioValue: '',
    refundReasons: ["不想买了", "拍错了", "卖家缺货", "我不喜欢", "其他"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.orderId = options.orderId
  },
  onShow: function () {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token
        }, () => {
          that.getOrderDes();
        })
      }
    })
  },
  getOrderDes: function () {
    var that = this;
    this.setData({
      isLoading: true
    })
    common.post('/order/getOrderDetail', {
      userId: that.data.userInfo.id,
      orderId: that.data.orderId,
      token: that.data.token
    },
      function (responseData) {
        if (responseData && responseData.status === 200) {
          for (let i = 0; i < responseData.data.length; i++) {
            if (responseData.data[i].orderId == that.data.orderId) {
              const orderTime = that.time(responseData.data[i].orderCreated)
              const paymentTime = that.time(responseData.data[i].paymentTime)
              that.setData({
                orderDes: responseData.data[i],
                orderTime: orderTime,
                paymentTime: paymentTime
              })
            }
            // 获取店铺信息
            that.getStoreInfo(responseData.data[i].storeId)
            break
          }
        } else {
          common.showRequestErrMsg(responseData)
        }
      },
      function () {
        that.setData({
          isLoading: false
        })
      }
    )
  },
  getStoreInfo(storeId) {
    common.post('/store/info', {
      storeId
    },
      (res) => {
        this.setData({
          storeInfo: res.data
        })
      })
  },
  time(time) {
    if (!time) {
      return '-'
    }
    var now = new Date(time);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    if (month <= 9) month = '0' + month;
    if (day <= 9) day = '0' + day;
    if (hours <= 9) hours = '0' + hours;
    if (minutes <= 9) minutes = '0' + minutes;
    var shu = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':00';
    return shu;
  },
  look(e) {
    const status = this.data.orderDes.status
    if (status == 2 || status == 4 || status == 5) {
      wx.navigateTo({
        url: '/pages/Goods/goodsOrder/goodsOrderQRCode/index?title=' + this.data.orderDes.title +
          '&orderNo=' + this.data.orderDes.orderNo +
          '&orderTime=' + this.data.orderTime +
          '&paymentTime=' + this.data.paymentTime +
          '&address=' + this.data.storeInfo.address +
          '&mobile=' + this.data.storeInfo.mobile +
          '&orderId=' + this.data.orderId +
          '&userId=' + this.data.userInfo.id,
      })
    } else {
      common.showToast('该订单不可查看二维码')
    }
  },
  close(e) {
    this.setData({
      code: true
    })
  },
  contact() {
    const mobile = this.data.storeInfo.mobile
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  gotoGoodsetail() {
    const idx = this.data.orderDes.itemId
    const title = this.data.orderDes.title
    const image = this.data.orderDes.picPath
    console.log(idx);
    wx.navigateTo({
      url: '/pages/Goods/GoodsDes/index?id=' + idx + '&name=' + title + '&image=' + image,
    })
  },
  showPostInfo() {

  },
  toStore(e) {
    common.gotoStore(this.data.storeInfo)
  },
  refund() {
    this.setData({
      showRefundDialog: true
    })
  },
  commitRefund() {
    if (this.data.refundRadioValue == '') {
      common.showToast('请选择退款原因')
      return
    }
    const that = this
    const param = {
      userId: that.data.userInfo.id,
      orderId: that.data.orderId,
      token: that.data.token,
      orderNo: this.data.orderDes.orderNo,
      orderItemId: this.data.orderDes.id,
      refundReason: this.data.refundReasons[this.data.refundRadioValue],
      refundAmount: this.data.orderDes.totalFee
    }
    common.post('/refund/order',
      param,
      function (res) {
        if (res.status === 200) {
          wx.redirectTo({
            url: '/pages/goodsOrder/Goods/goodsOrderRefund/index',
          })
        } else {
          common.showRequestErrMsg(res)
        }
      }
    )
  },
  onClose() {
    this.setData({
      showRefundDialog: false
    })
  },
  onChange(e) {
    console.log(e)
    this.setData({
      refundRadioValue: e.detail,
    })
  }
})